<?php
/**
 * @Author:      Zy
 * @DateTime:    2018-5-09 15:32:48
 * @Description: 用于 pms 订货单,采购单统计 检索
 */
class PMSStatisSearchController extends BaseController {

    /**
     * @Author:          sqq
     * @DateTime:        2018-05-09 下午13:30
     * @Description:     数据统计    [ 门店信息检索 ]
     * @param string $token             用户token
     * @return   [id]     [name]      [status]
     * @link http://api.anhui.com/v1/pms/StatisSearch/ShopListSearch?token=d4d69b726f2ee8795a9b1bf22112a51b
     */
    public function ShopListSearch(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("pms_error.args_error_100000");
                break;
            }
            $res = $this->verificationPMSUserToken(Input::get("token"),$response);
            if ($res['status'] == 0) {
                $response = $res['response'];
                break;
            }
            $shoplist = MaintenanceShops::select("id","name","status")->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => ["shoplist" => $shoplist]);
        }while(false);
            Log::info("info", array("system" => "pms", "event" => "StatisSearch/ShopListSearch", "input" => Input::all(), "output" => $response));
            return json_encode($response);
    }


    /**
     * @Author:          sqq
     * @DateTime:        2018-05-09 下午15:00
     * @Description:     数据统计    [ 供应商1检索 ]
     * @param string $token             用户token
     * @param string $name            供应商名称
     * @return   [id]     [name]      [purchase_status]
     * @link http://api.auhui.com/v1/pms/StatisSearch/SupplierSearch?token=d4d69b726f2ee8795a9b1bf22112a51b&name=昌平
     */
    public function SupplierSearch(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("pms_error.args_error_100000");
                break;
            }
            $res = $this->verificationPMSUserToken(Input::get("token"),$response);
            if ($res['status'] == 0) {
                $response = $res['response'];
                break;
            }
            $supplierlist = MaintenanceSupplier::select("id","name","purchase_status");
            if(Input::get("name")!=""){
                $supplierlist=$supplierlist->where('name','like',"%".trim(Input::get('name'))."%");
            }
            $supplierlist = $supplierlist->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => ["supplierlist" => $supplierlist]);
        }while(false);
        Log::info("info", array("system" => "pms", "event" => "StatisSearch/ShopListSearch", "input" => Input::all(), "output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:          sqq
     * @DateTime:        2018-05-09 下午15:37
     * @Description:     数据统计    [ 市区联动检索 ]
     * @param string $token             用户token
     * @param string $city_name         城市名称
     * @return   [city_id]     [city_name]      [region_name]  [id]
     * @link http://api.anhui.com/v1/pms/StatisSearch/city_region?token=d4d69b726f2ee8795a9b1bf22112a51b&city_name=上
     */
    public function city_region(){
        $validator = Validator::make(Input::all(), [
            'token' => 'required',
        ]);
        $response = [
            'code' => 0,
            'msg' => 'ok',
        ];

        do{
            if($validator->fails()) {
                $response = Lang::get("pms_error.args_error_100000");
                break;
            }
            $res = $this->verificationPMSUserToken(Input::get("token"),$response);
            if ($res['status'] == 0) {
                $response = $res['response'];
                break;
            }
            $res_list = [];
            $city_list = DataResourceCity::select('id', 'city_name', 'province_id')
                ->orderBy('id', 'ASC');
            if(Input::get('city_name') != ""){
                $city_list=$city_list->where('city_name','like',"%".trim(Input::get('city_name'))."%");
            }
            $city_list = $city_list->get()->toArray();
            $region_list = DataResourceRegion::select('id', 'region_name', 'city_id')
                ->orderBy('id', 'ASC')
                ->get()
                ->toArray();
            if(!empty($city_list)){
                if(!empty($region_list)){
                    $region_type = TRUE;
                } else {
                    $region_type = FALSE;
                }
                $tag_c = 0;
                foreach ($city_list as $k_c => $city) {
                    $res_list[$k_c]['city_id'] = $city['id'];
                    $res_list[$k_c]['city_name'] = $city['city_name'];
                    if($region_type){
                        foreach ($region_list as $k_r => $region) {
                            if($region['city_id'] == $city['id']){
                                $res_list[$k_c]['regions'][] = $region;
                                unset($region_list[$k_r]);
                            }
                        }
                    }
                    unset($city_list[$k_c]);
                    $tag_c++;
                }
            }
            $response['result'] = $res_list;
        } while (FALSE);
        Log::info('info', ['system' => 'pms', 'event' => 'supplier/get_province_city_region', 'input' => Input::all(), 'output' => $response]);
        return json_encode($response);
    }

    /**
     * @Author:          sqq
     * @DateTime:        2018-05-09 下午15:37
     * @Description:     数据统计    [ 市检索 ]
     * @param string $token             用户token
     * @param string $name         城市名称
     * @param int $num          一次展示的数量 [ 默认为50 ]
     * @return   [id]     [city_name]
     * @link http://api.anhui.com/v1/pms/StatisSearch/CitySearch?token=d4d69b726f2ee8795a9b1bf22112a51b&name=天
     */
    public function CitySearch(){
        $validator = Validator::make(Input::all(), [
            'token' => 'required',
        ]);
        $response = [
            'code' => 0,
            'msg' => 'ok',
        ];

        do{
            if($validator->fails()) {
                $response = Lang::get("pms_error.args_error_100000");
                break;
            }
            $res = $this->verificationPMSUserToken(Input::get("token"),$response);
            if ($res['status'] == 0) {
                $response = $res['response'];
                break;
            }
            $name = Input::get('name', '');
            $num = Input::get('num', 50);

            $list = DataResourceCity::select('id', 'city_name');
            if(!empty($name)){
                $list = $list->where('city_name', 'like', '%'.$name.'%');
            }
            $list = $list->skip(0)->take($num)
                ->orderBy('id', 'ASC')
                ->get();

            $response['result'] = $list;
        } while (FALSE);

        Log::info('info', ['system' => 'pms', 'event' => 'supplier/get_city', 'input' => Input::all(), 'output' => $response]);
        return json_encode($response);
    }


}